// packagePay/pages/employeebill/employeebill.js
import {
    getemployeesalaryrecordapi,
    getemployeesalaryheadapi,
    exportemployeesalaryrecordapi,
    stopPay,
    updateStatusapi
} from '../../api/pay'
import dayjs from 'dayjs'
import user from '../../../utils/user.js'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusflag: 0,
        typeindex: 0,
        msginfo: {},
        data: [],
        typearrarray: ['全部', '发薪', '预支'],
        typeinfoarr: [{
            name: '全部',
            value: ''
        }, {
            name: '发薪',
            value: '0'
        }, {
            name: '预支',
            value: '1'
        }],
        typeflag: '',
        statusarray: ['全部', '已领薪', '待领薪', '停止发薪', '领薪失败', '进行中'],
        listtabarr: [{
            name: '全部',
            index: 0,
            active: true,
            value: ''
        }, {
            name: '已领薪',
            index: 1,
            active: false,
            value: 1
        }, {
            name: "待领薪",
            index: 2,
            active: false,
            value: 0
        }, {
            name: "停止发薪",
            index: 3,
            active: false,
            value: 2
        }, {
            name: "领薪失败",
            index: 4,
            active: false,
            value: 4
        }, {
            name: "进行中",
            index: 5,
            active: false,
            value: 3
        }],
        pageSize: 10,
        pageNum: 1,
        status: '',
        month: "",
        idCardNo: "",
        hasnextpage: true,
        total: 0,
        m: 0,
        isSeatAdmin: null,
        isAdvance: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);
        user.waitLogin().then(() => {
            this.setData({
                isSeatAdmin: user.isSeatAdmin
            })
        })
        this.setData({
            month: options.month,
            idCardNo: options.idCardNo,
            m: dayjs(options.month).month() + 1
        }, () => {
            this.getemployeesalaryrecord()
            this.getemployeesalaryhead()
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    onJump(event) {
        console.log(event, 'onJumponJump')
        let id = event.currentTarget.dataset.id
        if (event.currentTarget.dataset.isadvance === 0) {
            wx.navigateTo({
                url: '/packagePay/pages/individual-detail/individual-detail?id=' + id,
            })
        } else {
            wx.navigateTo({
                url: '../../../packageAdvance/pages/advanceDetail/advanceDetail?id=' + e.currentTarget.dataset.id + '&istwo=1',
            })
        }
    },
    onStop(event) {
        if (event.currentTarget.dataset.isadvance === 0) {
            app.confirm('是否确认停止该员工发薪').then(() => {
                stopPay({
                    id: event.currentTarget.dataset.id
                }).then(() => {
                    app.toast('停止发薪成功', 'success')
                    setTimeout(() => {
                        this.setData({
                            pageNum: 1,
                            data: []
                        }, () => {
                            this.getemployeesalaryrecord()
                            this.getemployeesalaryhead()
                        })
                    }, 1000)

                })
            })
        } else {
            app.confirm('是否确认停止该员工预支').then(() => {
                updateStatusapi({
                    id: event.currentTarget.dataset.id
                }).then(() => {
                    app.toast('停止发薪成功', 'success')
                    setTimeout(() => {
                        this.setData({
                            pageNum: 1,
                            data: []
                        }, () => {
                            this.getemployeesalaryrecord()
                            this.getemployeesalaryhead()
                        })
                    }, 1000)
                })
            })
        }

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    export () {
        exportemployeesalaryrecordapi({
            queryMonth: this.data.month,
            idCardNo: this.data.idCardNo,
        }).then((res) => {
            console.log(res, '12345678');
            const url = `${app.globalData.baseUrl}/common/download?fileName=${encodeURIComponent(res)}&delete=false`
            if (wx.canIUse('openDocument')) {
                wx.downloadFile({
                    url,
                    success: function (res) {
                        wx.openDocument({
                            showMenu: true,
                            filePath: res.tempFilePath,
                            fileType: 'xlsx'
                        })
                    }
                })
            } else {
                this.setData({
                    copyLinkStatus: true,
                    copyLinkContent: url
                })
            }
        })
    },
    getemployeesalaryhead() {
        getemployeesalaryheadapi({
            idCardNo: this.data.idCardNo,
            queryMonth: this.data.month
        }).then((res) => {
            console.log(res, 'uhfgdsigyds');
            res.idcard4 = res.idCardNo.substr(-4)
            this.setData({
                msginfo: res
            })
        })
    },
    makecall(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.num,
        })
    },
    getemployeesalaryrecord() {
        console.log(this.data.status);
        let obj1 = {
            idCardNo: this.data.idCardNo,
            queryMonth: this.data.month,
            status: this.data.status,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize,
            isAdvance: this.data.isAdvance
        }
        let obj2 = {
            idCardNo: this.data.idCardNo,
            queryMonth: this.data.month,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize,
            isAdvance: this.data.isAdvance
        }
        getemployeesalaryrecordapi(this.data.status !== "" ? obj1 : obj2).then((res) => {
            console.log(res, 'list');
            if (!res.hasNextPage) {
                this.data.hasnextpage = false
            } else {
                this.data.hasnextpage = true
            }
            let arr = []
            arr.push(...this.data.data)
            arr.push(...res.list)
            this.setData({
                data: arr,
                hasnextpage: res.hasNextPage,
                total: res.total
            })
        })
    },
    changelisttab(e) {
        console.log(e.detail.value);
        this.data.listtabarr.forEach((ele, index) => {
            ele.active = false
        })
        let i = e.detail.value * 1
        this.data.listtabarr[i].active = !this.data.listtabarr[i].active
        console.log(this.data.listtabarr);
        this.setData({
            listtabarr: this.data.listtabarr,
            statusflag: i,
            status: this.data.listtabarr[i].value,
            data: [],
            pageNum: 1,
        })
        this.getemployeesalaryrecord()
    },
    bindPickerChange(e) {
        console.log(e.detail.value);
        this.setData({
            data: [],
            pageNum: 1,
            typeindex: e.detail.value,
            isAdvance: e.detail.value === '0' ? '' : e.detail.value === '1' ? 0 : 1
        })
        this.getemployeesalaryrecord()
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.data.hasnextpage) {
            this.setData({
                pageNum: this.data.pageNum + 1
            }, () => {
                this.getemployeesalaryrecord()
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})